package com.lzh.xianguang.controller;

import com.lzh.xianguang.common.AjaxResult;
import com.lzh.xianguang.model.UsersInfo;
import com.lzh.xianguang.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @Question UsersController
 * @Date 2023/3/5 10:03
 * @Solution
 */
@RestController
public class UsersController {
    @Autowired
    UsersService usersService;
    @RequestMapping("/reg")
    public Object reg(String avatarUrl, String nickname, String phone, String password) {
        int ret=usersService.add(avatarUrl,nickname,phone,password);
        if(ret==-1) {
            return AjaxResult.fail(-1,"该手机号已注册");
        } else {
            return AjaxResult.success(200);
        }
    }
    //登录
    @RequestMapping("/login")
    public Object login(String phone, String password) {

        //校验密码
        UsersInfo usersInfo = usersService.getByPhone(phone,password);
        if(usersInfo==null) {
            return AjaxResult.fail(-2,"用户名或密码错误");
        } else {
            return AjaxResult.success(usersInfo.getId());
        }
    }
}
